//
//  ZYLookLeaderboardViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/8/27.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "ZYLookLeaderboardViewController.h"

#import "ZYLookLeaderboardModel.h"

#import "BRankListTopCell.h"//顶部排行
#import "BRankListMineCell.h"//我的排名
#import "ZYLookLeaderboardCell.h"//其他排名

@interface ZYLookLeaderboardViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *muarr;
@property (nonatomic, strong) ZYLookLeaderboardModel *mineRankModel;
@property (nonatomic, strong) UIView *viewFooter;//到底啦提示
@end

@implementation ZYLookLeaderboardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"排行榜";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];

    self.muarr = [NSMutableArray arrayWithCapacity:1];
    
    [self ininWithUI];
    
    [self requestData];
    
    // Do any additional setup after loading the view.
}

- (void)ininWithUI {
    self.view.backgroundColor = kColorWithHex(0xffbe00);
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT - kNaviHeight)style:UITableViewStylePlain];
    _tableView.backgroundColor = kColorWithHex(0xffffff);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[ZYLookLeaderboardCell class] forCellReuseIdentifier:@"ZYLookLeaderboardCell"];
    [_tableView registerClass:[BRankListTopCell class] forCellReuseIdentifier:NSStringFromClass([BRankListTopCell class])];
    [_tableView registerClass:[BRankListMineCell class] forCellReuseIdentifier:NSStringFromClass([BRankListMineCell class])];
    
    //数据为空
    _tableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:CGRectMake(KWIDTH/2-50, KHEIGHT/2-36-60, 100, 72) image:imgname(@"tableviewPlaseholder") viewClick:^{
    }];
    _tableView.placeHolderView.backgroundColor = [UIColor clearColor];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.muarr.count > 0) {
        if (section == 0) {
            return 2;
        }else{
            return self.muarr.count;
        }
    }else{
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.muarr.count > 0 && section == 1) {
        return 57;
    }else{
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return [BRankListTopCell defaultHeight];
        }else if (indexPath.row == 1){
            return [BRankListMineCell defaultHeight];
        }else{
            return 0;
        }
    }else{
        return 47;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [UIView new];
    headerView.backgroundColor = kColorWithHex(0xffffff);
    if (section == 1 && self.muarr.count > 3) {
        headerView.frame = CGRectMake(0, 0, KWIDTH, 57);
        UILabel *lblRank = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width / 2.0, 56)];
        lblRank.text = @"排名";
        lblRank.font = [UIFont systemFontOfSize:16];
        lblRank.textColor = kColorWithHex(0x888888);
        lblRank.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:lblRank];
        UILabel *lblAddress = [[UILabel alloc] initWithFrame:CGRectMake(lblRank.width, 0, kScreen_Width / 2.0, 56)];
        lblAddress.text = @"区域";
        lblAddress.font = [UIFont systemFontOfSize:16];
        lblAddress.textColor = kColorWithHex(0x888888);
        lblAddress.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:lblAddress];
        
        UILabel *lblLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 56, kScreen_Width, 1)];
        lblLine.backgroundColor = kColorWithHex(0xe2e2e2);
        [headerView addSubview:lblLine];
    }
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            BRankListTopCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BRankListTopCell class]) forIndexPath:indexPath];
            cell.rankListArray = self.muarr;
            return cell;
        }else{
            BRankListMineCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BRankListMineCell class]) forIndexPath:indexPath];
            cell.model = self.mineRankModel;
            return cell;
        }
    }else{
        ZYLookLeaderboardCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYLookLeaderboardCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        ZYLookLeaderboardModel *model = [self.muarr objectAtIndexSafe:indexPath.row];
        cell.model = model;
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"当前cell不能点击哦");
}
#pragma mark - 数据请求方法
- (void)requestData {
    
    [NetWorkTool POST:PHBgetRankList param:nil success:^(id dic) {
        [self.muarr removeAllObjects];
        NSDictionary *mydic = [dic objectForKey:@"data"];
        
        NSArray *ranlList = [mydic objectForKey:@"rankList"];
        [ranlList enumerateObjectsUsingBlock:^(NSDictionary  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ZYLookLeaderboardModel *model = [ZYLookLeaderboardModel mj_objectWithKeyValues:obj];
            [self.muarr addObject:model];
        }];
        
        NSDictionary *selfDic = [mydic objectForKey:@"self_rank"];
        self.mineRankModel = [ZYLookLeaderboardModel mj_objectWithKeyValues:selfDic];
        
        //暂时没有分页处理
        self.tableView.tableFooterView = self.viewFooter;
        if (self.muarr.count > 0) {
            self.tableView.backgroundColor = kColorWithHex(0xffbe00);
        }
        [self.tableView reloadData];
        
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
    } needUser:YES];
}

#pragma mark - 懒加载
- (UIView *)viewFooter{
    if (!_viewFooter) {
        _viewFooter = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 96)];
        _viewFooter.backgroundColor = [UIColor clearColor];
        UIView *viewTop = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 47)];
        viewTop.backgroundColor = kColorWithHex(0xffffff);
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:viewTop.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = viewTop.bounds;
        maskLayer.path = maskPath.CGPath;
        viewTop.layer.mask = maskLayer;
        [_viewFooter addSubview:viewTop];
        UIImageView *ivNotice = [[UIImageView alloc] initWithFrame:CGRectMake((kScreen_Width - 100) / 2.0, 67, 100, 17)];
        ivNotice.image = imgname(@"rank_data_end");
        [_viewFooter addSubview:ivNotice];
    }
    return _viewFooter;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
